package com.study.studyroom.utils;

import com.study.studyroom.entity.bo.RoomBo;
import com.study.studyroom.entity.enums.RequestResultEnum;
import com.study.studyroom.entity.enums.UnitTypeEnum;
import com.study.studyroom.entity.exception.GlobalException;

import java.math.BigDecimal;
import java.util.Date;

public class PriceUtil {

    public static BigDecimal getPrice(RoomBo roomBo, Date startTime,Date endTime){
        long closeTime = endTime.getTime();
        long openTime = startTime.getTime();
        if (openTime>closeTime){
            throw new GlobalException(RequestResultEnum.TIME_ERROR);
        }
        long useTime = closeTime- openTime;
        UnitTypeEnum unitTypeEnum = UnitTypeEnum.getByCode(Integer.parseInt(roomBo.getUnitType()));
        long formula = unitTypeEnum.getFormula();
        BigDecimal userUnitCount = new BigDecimal(useTime/ formula);
        BigDecimal price = userUnitCount.multiply(roomBo.getUnitPrice()).add(roomBo.getStartPrice());
        return price.compareTo(roomBo.getMaxPrice())>=0?roomBo.getMaxPrice():price;
    }
}
